//
//  IntegralViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/6/29.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "IntegralViewController.h"
#import "IntegralCell.h"

#import "MineModel.h"
#import "MineViewModel.h"

@interface IntegralViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource
>
{
    UILabel * _nameLabel;
}
@property (nonatomic,strong) UIImageView * headView;
@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) MineViewModel * mineViewModel;
@end

@implementation IntegralViewController

-(MineViewModel *) mineViewModel{
    if (!_mineViewModel) {
        _mineViewModel = [[MineViewModel alloc] init];
    }
    return _mineViewModel;
}
-(UITableView *) tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DEF_DEVICE_WIDTH, DEF_CONTENT_INTABBAR_HEIGHT) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setTableHeaderView:[self headView]];
        
        [MXUtility setExtraCellLineHidde:_tableView tabHeader:YES tabFooter:NO];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem setTitle:@"积分中心"];
    
    [self.view addSubview:self.tableView];

    [self integralNetWork];
}

-(void) integralNetWork{
    
    __weak typeof (self) weakSelf = self;

    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [MXUtility showMBProgress:weakSelf.view message:LoadingText];
        [[weakSelf.mineViewModel showCoreDetailRequestParameter] subscribeNext:^(id x) {
            
            _nameLabel.text = [NSString stringWithFormat:@"当前积分：%@",self.mineViewModel.userScore];
            
            
            [weakSelf.tableView reloadData];
            [MXUtility hideMBProgress:weakSelf.view];
            [weakSelf.tableView.mj_header endRefreshing];
        }error:^(NSError *error) {
            
            [weakSelf.tableView.mj_header endRefreshing];
            [MXUtility hideMBProgress:weakSelf.view];
        }];

    }];
    
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer.hidden =YES;
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.mineViewModel.showCoreArrayList count];
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 51;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * str = @"cell";
    IntegralCell * cell  = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"IntegralCell" owner:self options:nil]lastObject
        ];
    }
    ShowCoreModel * showModel = [self.mineViewModel.showCoreArrayList objectAtIndex:indexPath.row];
    [cell setIntegralDataSource:showModel];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(UIImageView *)headView{
    if (!_headView) {
        _headView=[[UIImageView alloc]init];
        _headView.frame=CGRectMake(0, 64, DEF_DEVICE_WIDTH, 160);
        _headView.backgroundColor=[UIColor clearColor];
        [_headView setImage:[UIImage imageNamed:@"jifenzhongxin"]];
        
        UIImageView * _headerImg=[[UIImageView alloc]initWithFrame:CGRectMake(DEF_DEVICE_WIDTH/2-35, 50, 70, 70)];
        _headerImg.center=CGPointMake(DEF_DEVICE_WIDTH/2, 70);
        [_headerImg sd_setImageWithURL:[NSURL URLWithString:UserInfoShare.userCenterModel.userImgPath] placeholderImage:DEF_NORMAL_DEFAULT_IMAGE];
        [_headerImg.layer setMasksToBounds:YES];
        [_headerImg.layer setCornerRadius:35];
        _headerImg.backgroundColor=[UIColor whiteColor];
        [_headView addSubview:_headerImg];
        
        _nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 130, DEF_DEVICE_WIDTH, 20)];
        _nameLabel.center = CGPointMake(DEF_DEVICE_WIDTH/2, 125);
        _nameLabel.userInteractionEnabled = YES;
        [_nameLabel setFont:[UIFont systemFontOfSize:15.f]];
        _nameLabel.textColor=[UIColor blackColor];
        _nameLabel.textAlignment=NSTextAlignmentCenter;
        [_headView addSubview:_nameLabel];
    }
    return _headView;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
